# -*- coding:utf-8 -*-
"""
网页下载器，负责下载html代码及图片
"""

import os
import re
import sys

import requests
from requests.adapters import HTTPAdapter


class HtmlDownloader(object):

    def __init__(self):
        requests.packages.urllib3.disable_warnings()

    @staticmethod
    def download(url):
        if url is None:
            return None
        print('get url:', url)
        try:
            s = requests.Session()
            s.mount('http://', HTTPAdapter(max_retries=3))
            response = s.get(url, timeout=15)
            # response = http.request('GET', url)
            if response.status_code != 200:
                return None
            return response.content
        except:
            print("Unexpected error:", sys.exc_info()[0])

    @staticmethod
    def download_pic(url):
        base_path = 'dist/'
        if url is None:
            return None
        file_name = base_path + re.findall(r'[\w]+_[\S]+.gif', url)[0]
        if os.path.exists(file_name):
            return None
        print('download:', url)
        path = os.path.split(file_name)[0]
        if not os.path.exists(path):
            os.makedirs(path)
        image_data = HtmlDownloader.download(url)
        with open(file_name, 'wb') as image_file:
            image_file.write(image_data)
            image_file.close()
            print('save image', image_file.name)
            return True
